<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Menu;
use App\Role;
use App\Admin;
use Hash;
use DB;
use Illuminate\Database\QueryException;

class roleController extends Controller
{
    static $tableName='roles';
     /**
     * 软删除Trait
     */
    use \App\Http\utils\trashedMethods;
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyWord=$request->input('keyWord');
        $is_trashed=$request->input('trashed');
        if($is_trashed){
            $pageListData=Role::where("role_name","like","%".$keyWord."%")->onlyTrashed()->orderbyDesc('sort')->orderByDesc('updated_at')->paginate(8);
        }else{
            $pageListData=Role::where("role_name","like","%".$keyWord."%")->orderbyDesc('sort')->orderByDesc('updated_at')->paginate(8);
        }
        return view('admin.role.index',compact('pageListData','is_trashed','keyWord'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $menuListData=Menu::getTree(0,1);
        return view('admin.role.form',compact('menuListData'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $schema=[
            'role_name'=>'required',
            'menu_ids'=>'required',
            'sort'=>'required',
        ];
        $request->validate($schema);
        $createData=$request->only(['role_name','menu_ids','sort']);
        Role::create($createData);
        return json(200,'创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $menuListData=Menu::getTree(0,1);
        $editData=Role::find($id);
        return view('admin.role.form',compact('editData','menuListData'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $schema=[
            'role_name'=>'required',
            'menu_ids'=>'required',
            'sort'=>'required',
        ];
        $request->validate($schema); 
        $updateData=$request->only(['role_name','menu_ids','sort']);

        //修改权限角色，更新使用当前权限角色的用户TOKEN
        $hashToken=Hash::make(time().mt_rand());
            try{
                DB::transaction(function()use($id,$hashToken,$updateData){
                        Admin::where('role_id',$id)->update(['token'=>$hashToken]);
                        Role::find($id)->update($updateData);
                });
            }
            catch(QueryException $exception){
                return json(105,'事务终止,修改失败，请联系管理员');
            }
            
            return json(200,'修改成功');
    
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res=Role::find($id)->delete();
        if($res) return json(200,'删除成功');
        return json(105,'删除失败');
    }
}
